// pages/index/mine.js
const App = getApp()

import Kits from '../../utils/kits'


Component({
  options: {
    addGlobalClass: true,
  },
  properties: {

  },
  data: {
    init: false,
    logined: false,
    user: {},
    summ: {
      couponStr: '--',
      amountStr: '--',
      settleStr: '--',
      depositStr: '--',
    },

    loginAlert: false,
    loginNext: '',

  },

  lifetimes: {
    attached() {

      this.initUI()

      wx.sub('ReloadUserInfo', this, () => {
        this.initUI()
      })
      wx.sub('ReloadUserFin', this, () => {
        this.initUI()
      })
    },
    detached() {
      wx.cancel('ReloadUserInfo', this)
      wx.cancel('ReloadUserFin', this)
    },
  },
  methods: {
    initUI() {
      this.setData({
        init: true,
        logined: App.Global.User.id != undefined && App.Global.User.id != ''
      })
      if (this.data.logined) {
        this.execLoadUser()
        this.execLoadFin()

      }
    },

    doPulldown() {
      this.triggerEvent('stopPullDown', {})
    },
    doReachBottom() {

    },
    execLoadUser() {
      App.doPost({
        uri: '/app/user/info/profile/info',
        body: {},
        fnSucc: resp => {
          this.setData({
            init: true,
            user: resp.data
          })
        }
      })
    },
    execLoadFin() {
      App.doPost({
        uri: '/app/wallet/fin/summary',
        body: {},
        fnSucc: resp => {
          resp.data.couponStr = (resp.data.coupon) + ''
          resp.data.amountStr = Kits.fmtMoneyString(resp.data.amount)
          resp.data.settleStr = Kits.fmtMoneyString(resp.data.settle)
          resp.data.depositStr = Kits.fmtMoneyString(resp.data.deposit)
          this.setData({
            summ: resp.data
          })
        }
      })
    },
    toHelp() {
      App.doPost({
        uri: '/app/base/data/config',
        body: {},
        fnSucc: resp => {
        //   let uri = resp.data.web.kfzx
        let uri = 'https://h5.hangzhouyizuke.com/service/#/?type=c'
          wx.navigateTo({
            url: `/pages/funcs/web?url=${encodeURIComponent(uri)}`,
          })
        }
      })
    },
    toPage(e) {
      wx.navigateTo({
        url: e.currentTarget.dataset.uri
      })
    },
    toLogin() {
      this.setData({
        loginAlert: false,
      })
      if (this.data.loginNext) {
        wx.navigateTo({
          url: `/pages/user/login?next=${encodeURIComponent(this.data.loginNext)}`,
        })
      } else {
        wx.navigateTo({
          url: '/pages/user/login',
        })
      }

    },
    doLogin(e) {
      this.setData({
        loginAlert: true,
        loginNext: e.currentTarget.dataset.uri
      })
    },
    noLogin() {
      this.setData({
        loginAlert: false,
      })
    },
  }
})